package com.keyinong.caizhengting.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.MyShowDialog;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.keyinong.caizhengting.tools.SystemBarTintManager;
import com.keyinong.caizhengting.activity.FanqiedingcanActivity;
import com.keyinong.caizhengting.activity.GoodsTwoSortActivity;
import com.keyinong.caizhengting.activity.QuedingdingdanActivity;
import com.keyinong.caizhengting.entity.GouwucheEntity;
import com.keyinong.caizhengting.view.widget.MyListView;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车
 */

public class ShoppingCartsFragment extends Fragment {

    static int geshu = 0;
    View mDialogView = null;
    HttpTools mHttpTools = null;
    private List<GouwucheEntity> mFoodList;
    private List<GouwucheEntity> mGoodsList;
    private ShoppingCartsAdapter mFoodsAdapter;
    private ShoppingCartsAdapter mGoodsAdapter;
    private FrameLayout mFlFoods;
    private FrameLayout mFlGoods;
    private RadioButton mRbFoods;
    private RadioButton mRbGoods;
    private LinearLayout mLlShoppingCarts;
    private MyListView mLvFoods;
    private MyListView mLvGoods;
    private TextView mTvTotalNum;
    private TextView mTvTotalPrice;
    private int mTotalNum;
    private double mTotalPrice = 0.0;
    private Button mBtnSubmit;
    private RelativeLayout mRlBottom;
    private ImageView mIvTips;
    private TextView mTvTips;
    private Button mBtnClean;
    private ImageButton mIbBack;

    private int mType;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            getActivity().getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        return inflater.inflate(R.layout.fragment_shopping_carts, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        SystemBarTintManager tintManager = new SystemBarTintManager(getActivity());
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintColor(getResources().getColor(R.color.white_1));
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mFlFoods = (FrameLayout) getView().findViewById(R.id.fl_type_foods);
        mFlGoods = (FrameLayout) getView().findViewById(R.id.fl_type_goods);

        mRbFoods = (RadioButton) getView().findViewById(R.id.rb_foods);
        mRbGoods = (RadioButton) getView().findViewById(R.id.rb_goods);

        mLvFoods = (MyListView) getView().findViewById(R.id.lv_foods);
        mLvGoods = (MyListView) getView().findViewById(R.id.lv_goods);

        mLlShoppingCarts = (LinearLayout) getView().findViewById(R.id.ll_shopping_carts);

        mTvTotalNum = (TextView) getView().findViewById(R.id.tv_total_num);
        mTvTotalPrice = (TextView) getView().findViewById(R.id.tv_total_price);

        mIvTips = (ImageView) getView().findViewById(R.id.iv_tips);
        mTvTips = (TextView) getView().findViewById(R.id.tv_tips);

        mRbFoods.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mRbGoods.isChecked()) {
                    mRbGoods.setChecked(false);
                    mLvGoods.setClickable(false);
                }

                mType = 0;
                mTotalNum = 0;
                mTotalPrice = 0;

                for (GouwucheEntity carts : mFoodList) {
                    mTotalNum += Integer.valueOf(carts.getAmount());
                    mTotalPrice += Double.valueOf(carts.getPrice()) * Integer.valueOf(carts.getAmount());
                }

                mTvTotalNum.setText(String.valueOf(mTotalNum));
                mTvTotalPrice.setText(String.valueOf(mTotalPrice));
            }
        });

        mRbGoods.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mRbFoods.isChecked()) {
                    mRbFoods.setChecked(false);
                    mLvFoods.setClickable(false);
                }

                mType = 1;
                mTotalNum = 0;
                mTotalPrice = 0;

                for (GouwucheEntity carts : mGoodsList) {
                    mTotalNum += Integer.valueOf(carts.getAmount());
                    mTotalPrice += Double.valueOf(carts.getPrice()) * Integer.valueOf(carts.getAmount());
                }

                mTvTotalNum.setText(String.valueOf(mTotalNum));
                mTvTotalPrice.setText(String.valueOf(mTotalPrice));
            }
        });

        mRlBottom = (RelativeLayout) getView().findViewById(R.id.rl_bottom);
        mBtnSubmit = (Button) getView().findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!mRbFoods.isChecked() && !mRbGoods.isChecked()) {
                    Toast.makeText(getActivity(), "请先选择一个类别进行支付！", Toast.LENGTH_SHORT).show();
                    return;
                }

                //商品是否已下架判断
                if (mRbFoods.isChecked()) {
                    for (int i = 0; i < mFoodList.size(); i++) {
                        if ("1".equals(mFoodList.get(i).getShelve())) {
                            Toast.makeText(getActivity(), "请先移除下架商品", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                }

                if (mRbGoods.isChecked()) {
                    for (int i = 0; i < mGoodsList.size(); i++) {
                        if ("1".equals(mGoodsList.get(i).getShelve())) {
                            Toast.makeText(getActivity(), "请先移除下架商品", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                }

                Intent intent = new Intent(getActivity(), QuedingdingdanActivity.class);
                intent.putExtra("type", mType);
                getActivity().startActivity(intent);
            }
        });
        //清空购物车
        mBtnClean = (Button) getView().findViewById(R.id.btn_qingkong);
        mBtnClean.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showCleanDialog();
            }
        });
        //返回
        mIbBack = (ImageButton) getView().findViewById(R.id.ib_fanhui);
        mIbBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), GoodsTwoSortActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 获取购物车信息
     */
    private void getShoppingCarts() {
        //网络判断
        if (!NetWorkUtil.isNetwork(getActivity())) {
            Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        mFoodList = new ArrayList<>();
        mGoodsList = new ArrayList<>();
        SharedPreferences sp = getActivity().getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        if (mHttpTools == null) {
            mHttpTools = new HttpTools(getActivity());
        }
        mHttpTools.getShoppingCat(token, new ShoppingCartsHandler());
    }

    @Override
    public void onResume() {
        super.onResume();
        mTotalPrice = 0.0;
        geshu = 0;

        getShoppingCarts();
    }

    /**
     * 支付弹窗
     */
    private void showCleanDialog() {

        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        mDialogView = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_pay, null);
        TextView tvTitle = (TextView) mDialogView.findViewById(R.id.tital);
        TextView tvNo = (TextView) mDialogView.findViewById(R.id.tv_no);
        TextView tvYes = (TextView) mDialogView.findViewById(R.id.tv_yes);
        tvTitle.setText("是否清空购物车？");
        dialog.setContentView(mDialogView);
        dialog.show();
        tvNo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvYes.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getActivity().getSharedPreferences("spReg", Context.MODE_PRIVATE);
                String token = sp.getString("data", "");
                if (mHttpTools == null) {
                    mHttpTools = new HttpTools(getActivity());
                }
                mHttpTools.cleanShoppingCat(token, new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers,
                                          JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        try {
                            boolean code = response.getBoolean("code");
                            String msg = response.getString("msg");
                            if (code) {
                                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();

                                mLlShoppingCarts.setVisibility(View.GONE);

                                mRlBottom.setVisibility(View.GONE);
                                mBtnSubmit.setVisibility(View.GONE);
                                mIvTips.setVisibility(View.VISIBLE);

                                setMsg("qingchu1", "0", 1);
                                dialog.dismiss();
                            } else {
                                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        });
    }

    private void setMsg(String sopt, String str, int index) {
        Message message = Message.obtain();
        Bundle bundle = new Bundle();
        bundle.putString("sopt1", sopt);
        bundle.putString("str1", str);
        bundle.putInt("index1", index);
        message.obj = bundle;
        FanqiedingcanActivity.myhanler.sendMessage(message);
    }

    public class ShoppingCartsHandler extends JsonHttpResponseHandler {
        @Override
        public void onStart() {
            MyShowDialog.showLoadingDialog(getActivity(), "请稍后....");
            super.onStart();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            Log.w("ShoppingCarts", response.toString());
            try {
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                MyShowDialog.closeLoadingDialog();
                initView();
                if (code) {
                    JSONObject object = response.getJSONObject("data");
                    JSONArray array = object.getJSONArray("shoppingcarts");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject data = array.getJSONObject(i);
                        String id = data.getString("id");
                        String productId = data.getString("productid");
                        String name = data.getString("name");
                        String content = data.getString("content");
                        String price = data.getString("price");
                        int type = data.getInt("type");
                        String shelve = data.getString("shelve");
                        String stock = data.getString("stock");
                        String perlim = data.getString("perlim");
                        String amount = data.getString("amount");
                        String buyamount = data.getString("buyamount");
                        if (type == 0) {
                            mFoodList.add(new GouwucheEntity(id, productId, name, content, price, amount, shelve, stock, perlim, buyamount, type));
                        } else if (type == 1) {
                            mGoodsList.add(new GouwucheEntity(id, productId, name, content, price, amount, shelve, stock, perlim, buyamount, type));
                        }
                    }

                    if (mFoodList.size() == 0 && mGoodsList.size() == 0) {
                        mIvTips.setVisibility(View.VISIBLE);
                        mTvTips.setVisibility(View.VISIBLE);
                        mRlBottom.setVisibility(View.GONE);
                        mBtnSubmit.setVisibility(View.GONE);
                        mLlShoppingCarts.setVisibility(View.GONE);
                    } else if (mFoodList.size() != 0 && mGoodsList.size() != 0) {
                        mIvTips.setVisibility(View.INVISIBLE);
                        mTvTips.setVisibility(View.INVISIBLE);
                        mRlBottom.setVisibility(View.VISIBLE);
                        mBtnSubmit.setVisibility(View.VISIBLE);
                        mFlFoods.setVisibility(View.VISIBLE);
                        mFlGoods.setVisibility(View.VISIBLE);
                    } else if (mFoodList.size() > 0) {
                        mIvTips.setVisibility(View.INVISIBLE);
                        mTvTips.setVisibility(View.INVISIBLE);
                        mRlBottom.setVisibility(View.VISIBLE);
                        mBtnSubmit.setVisibility(View.VISIBLE);
                        mFlFoods.setVisibility(View.VISIBLE);
                    } else {
                        mIvTips.setVisibility(View.INVISIBLE);
                        mTvTips.setVisibility(View.INVISIBLE);
                        mRlBottom.setVisibility(View.VISIBLE);
                        mBtnSubmit.setVisibility(View.VISIBLE);
                        mFlGoods.setVisibility(View.VISIBLE);
                    }
                    mFoodsAdapter = new ShoppingCartsAdapter(getActivity(), mFoodList);
                    mLvFoods.setAdapter(mFoodsAdapter);
                    mLvFoods.setDivider(null);
                    mGoodsAdapter = new ShoppingCartsAdapter(getActivity(), mGoodsList);
                    mLvGoods.setAdapter(mGoodsAdapter);
                    mLvGoods.setDivider(null);
                } else {
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 购物车适配器
     */
    public class ShoppingCartsAdapter extends BaseAdapter {
        Context mContext;
        List<GouwucheEntity> mCartsList;
        HttpTools mHttpTools = null;

        public ShoppingCartsAdapter(Context mContext, List<GouwucheEntity> mCartsList) {
            super();
            this.mContext = mContext;
            this.mCartsList = mCartsList;
        }

        @Override
        public int getCount() {
            return mCartsList.size();
        }

        @Override
        public Object getItem(int position) {
            return mCartsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHandle handle;
            if (view == null) {
                handle = new ViewHandle();
                view = LayoutInflater.from(mContext).inflate(R.layout.item_gouwuche, null);
                handle.mTvGoodsName = (TextView) view.findViewById(R.id.tv_shangpinming2);
                handle.mTvContent = (TextView) view.findViewById(R.id.tv_content2);
                handle.mTvPrice = (TextView) view.findViewById(R.id.tv_price2);
                handle.mIbAdd = (ImageButton) view.findViewById(R.id.ib_add2);
                handle.mIbMinus = (ImageButton) view.findViewById(R.id.ib_less2);
                handle.mIbDelete = (ImageButton) view.findViewById(R.id.ib_delete);
                handle.mEtAmount = (EditText) view.findViewById(R.id.et_amount2);
                handle.mTvLimit = (TextView) view.findViewById(R.id.tv_lim);
                view.setTag(handle);
            } else {
                handle = (ViewHandle) view.getTag();
            }
            if ("1".equals(mCartsList.get(position).getShelve())) {
                handle.mTvLimit.setVisibility(View.VISIBLE);
                handle.mTvLimit.setText("商品已下架");
                setMsg("shelve", "1", position);
            } else {
                handle.mTvLimit.setVisibility(View.GONE);
            }
            handle.mTvGoodsName.setText(mCartsList.get(position).getName());
            handle.mTvContent.setText(mCartsList.get(position).getContent());
            handle.mTvPrice.setText(mCartsList.get(position).getPrice());

            //数量
            handle.mEtAmount.setText(mCartsList.get(position).getAmount());

            //加
            handle.mIbAdd.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int goodsNum = Integer.parseInt(handle.mEtAmount.getText().toString());
                    goodsNum++;
                    addGoods(position, handle.mEtAmount, goodsNum, handle.mTvLimit);
                }
            });
            //减
            handle.mIbMinus.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int goodsNum = Integer.parseInt(handle.mEtAmount.getText().toString());
                    goodsNum--;
                    if (goodsNum <= 0) {
                        removeGoods(position);
                        return;
                    }

                    handle.mEtAmount.setText(String.format("%s", goodsNum));
                    minusGoods(position, handle.mEtAmount, goodsNum, handle.mTvLimit);
                }
            });
            //删除
            handle.mIbDelete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    removeGoods(position);

                }
            });
            return view;
        }

        /**
         * 增加商品
         */
        private void addGoods(final int index, final EditText editText, final int goodsNum, final TextView tv) {
            if (!NetWorkUtil.isNetwork(mContext)) {
                Toast.makeText(mContext, "当前网络不可用", Toast.LENGTH_SHORT).show();
                return;
            }
            if ("".equals(editText.getText().toString())) {
                editText.setText(String.format("%s", 0));
            }
            SharedPreferences sp = mContext.getSharedPreferences("spReg",
                    Context.MODE_PRIVATE);
            String token = sp.getString("data", "");
            if (mHttpTools == null) {
                mHttpTools = new HttpTools(mContext);
            }
            mHttpTools.addgoodscat(token, mCartsList.get(index).getProductid(), String.valueOf(goodsNum),
                    new JsonHttpResponseHandler() {

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            Log.w("ADD", response.toString());
                            if (mRbFoods.isChecked() || mRbGoods.isChecked()) {
                                try {
                                    boolean code = response.getBoolean("code");
                                    String msg = response.getString("msg");
                                    if (code) {
                                        JSONObject data = response.getJSONObject("data");
                                        int totalAmount = data.getInt("amount");
                                        double totalPrice = data.getDouble("total");
                                        GouwucheEntity carts = mCartsList.get(index);
                                        carts.setAmount(String.valueOf(goodsNum));
                                        editText.setText(String.valueOf(goodsNum));
                                        double price = Double.valueOf(carts.getPrice());

                                        mTotalNum += 1;
                                        mTotalPrice = mTotalPrice + 1 * price;

                                        mTvTotalNum.setText(String.valueOf(mTotalNum));
                                        mTvTotalPrice.setText(String.valueOf(mTotalPrice));

                                        setMsg1("jia", String.valueOf(totalAmount), index);
                                        setMsg1("zongjia", String.valueOf(totalPrice), 0);
                                    } else {
                                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    });
        }

        /**
         * 减少商品
         */
        private void minusGoods(final int index, final EditText editText, int goodsNum, final TextView tv) {
            if (!NetWorkUtil.isNetwork(mContext)) {
                Toast.makeText(mContext, "当前网络不可用", Toast.LENGTH_SHORT).show();
                return;
            }
            if ("".equals(editText.getText().toString())) {
                editText.setText(String.format("%s", 0));
                goodsNum = 0;
            }
            SharedPreferences sp = mContext.getSharedPreferences("spReg",
                    Context.MODE_PRIVATE);
            String token = sp.getString("data", "");

            if (mHttpTools == null) {
                mHttpTools = new HttpTools(mContext);
            }
            mHttpTools.addgoodscat(token, mCartsList.get(index).getProductid(), String.valueOf(goodsNum),
                    new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            Log.w("MINUS", response.toString());
                            if (mRbFoods.isChecked() || mRbGoods.isChecked()) {
                                try {
                                    boolean code = response.getBoolean("code");
                                    String msg = response.getString("msg");
                                    int goodsNum = Integer.valueOf(editText.getText().toString());
                                    if (code) {
                                        JSONObject data = response.getJSONObject("data");
                                        int totalAmount = data.getInt("amount");
                                        double totalPrice = data.getDouble("total");
                                        GouwucheEntity carts = mCartsList.get(index);
                                        carts.setAmount(String.valueOf(goodsNum));
                                        editText.setText(String.valueOf(goodsNum));
                                        double price = Double.valueOf(carts.getPrice());

                                        mTotalNum -= 1;
                                        mTotalPrice = mTotalPrice - 1 * price;

                                        mTvTotalNum.setText(String.valueOf(mTotalNum));
                                        mTvTotalPrice.setText(String.valueOf(mTotalPrice));

                                        setMsg1("jian", String.valueOf(totalAmount), index);
                                        setMsg1("zongjia", String.valueOf(totalPrice), 0);
                                    } else {
                                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    });
        }

        /**
         * 移除商品
         */
        private void removeGoods(final int index) {
            if (!NetWorkUtil.isNetwork(mContext)) {
                Toast.makeText(mContext, "当前网络不可用", Toast.LENGTH_SHORT).show();
                return;
            }
            SharedPreferences sp = mContext.getSharedPreferences("spReg",
                    Context.MODE_PRIVATE);
            String token = sp.getString("data", "");

            if (mHttpTools == null) {
                mHttpTools = new HttpTools(mContext);
            }
            mHttpTools.deletegoods(token, mCartsList.get(index).getId(),
                    new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            Log.w("DELETE", response.toString());
                            try {
                                boolean code = response.getBoolean("code");
                                String msg = response.getString("msg");
                                if (code) {
                                    Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                                    JSONObject data = response.getJSONObject("data");
                                    int totalAmount = data.getInt("amount");
                                    int type = mCartsList.get(index).getType();

                                    if (type == 0) {
                                        mFoodList.remove(index);
                                        mFoodsAdapter.notifyDataSetChanged();

                                        if (mFoodList.size() == 0) {
                                            mFlFoods.setVisibility(View.GONE);
                                        }

                                        mTotalNum = 0;
                                        mTotalPrice = 0;

                                        for (GouwucheEntity cart : mFoodList) {
                                            mTotalNum += Integer.valueOf(cart.getAmount());
                                            mTotalPrice += Double.valueOf(cart.getPrice())
                                                    * Integer.valueOf(cart.getAmount());
                                        }

                                    } else {
                                        mGoodsList.remove(index);
                                        mGoodsAdapter.notifyDataSetChanged();

                                        if (mGoodsList.size() == 0) {
                                            mFlGoods.setVisibility(View.GONE);
                                        }

                                        mTotalNum = 0;
                                        mTotalPrice = 0;

                                        for (GouwucheEntity cart : mGoodsList) {
                                            mTotalNum += Integer.valueOf(cart.getAmount());
                                            mTotalPrice += Double.valueOf(cart.getPrice())
                                                    * Integer.valueOf(cart.getAmount());
                                        }
                                    }

                                    if (mFoodList.size() == 0 && mGoodsList.size() == 0) {
                                        mIvTips.setVisibility(View.VISIBLE);
                                        mRlBottom.setVisibility(View.GONE);
                                        mBtnSubmit.setVisibility(View.GONE);
                                    } else {
                                        mRlBottom.setVisibility(View.VISIBLE);
                                        mBtnSubmit.setVisibility(View.VISIBLE);
                                        mIvTips.setVisibility(View.INVISIBLE);
                                    }

                                    mTvTotalNum.setText(String.valueOf(mTotalNum));
                                    mTvTotalPrice.setText(String.valueOf(mTotalPrice));

                                    setMsg1("qingchu", String.valueOf(totalAmount), index);
                                } else {
                                    Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }

        /**
         * 购物车角标
         */
        private void setMsg1(String sopt, String str, int index) {
            Message message = Message.obtain();
            Bundle bundle = new Bundle();
            bundle.putString("sopt", sopt);
            bundle.putString("str", str);
            bundle.putInt("index", index);
            message.obj = bundle;
            FanqiedingcanActivity.myhanler1.sendMessage(message);
        }

        class ViewHandle {
            /**
             * 商品名
             */
            TextView mTvGoodsName;
            /**
             * 说明
             */
            TextView mTvContent;
            /**
             * 价格
             */
            TextView mTvPrice;
            /**
             * 加
             */
            ImageButton mIbAdd;
            /**
             * 减
             */
            ImageButton mIbMinus;
            /**
             * 删除
             */
            ImageButton mIbDelete;
            /**
             * 数量
             */
            EditText mEtAmount;
            /**
             * 库存
             */
            TextView mTvLimit;
        }
    }

}

